<?php

namespace App\Console\Commands;

use App\Helpers\Utils;
use App\Models\Building;
use App\Models\Department;
use App\Models\Dorm;
use App\Models\DormMeter;
use App\Models\MeterType;
use App\Models\Period;
use App\Models\PermissionCategory;
use App\Models\Role;
use App\Models\Staff;
use App\Models\StaffPosition;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Console\Command;

class BuildTests extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'build:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'insert test data for quick test';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $q = $this->ask('truncate staffs?','n');
        if(strtolower($q)=='y'){
            \DB::unprepared('SET FOREIGN_KEY_CHECKS=0');
            Staff::truncate();
        }

        $q = $this->ask('insert staff test data?','n');
        if(strtolower($q)=='y'){
            //插入不同职位的员工不同性别的职工
            $pos = StaffPosition::all();
            $dpts = Department::all();
            $genders = [1=>'男',2=>'女'];
            foreach ($dpts as $dpt) {
                foreach ($pos as $k=>$po){
                    $sn = str_pad($k+1,2,0,STR_PAD_LEFT);
                    $en = str_pad($dpt->id,2,0,STR_PAD_LEFT);
                    $id_card = "452126198807$sn"."10$en";
                    foreach ($genders as $i=>$gender) {
                        $arr =[
                            'staff_position_id'=>$po->id,
                            'sex'=> $i,
                            'name'=>$gender.$po->position.$dpt->id,
                            'department_id'=>$dpt->id,
                            'id_card'=>$id_card,
                            'card_num'=>"A$dpt->id"."00$k".$i
                        ];

                        Staff::create($arr);
                    }

                }
            }
        }

        $q = $this->ask('update building gen_dorm_rule?','n');
        if(strtolower($q)=='y'){
            $buildings = Building::all();
            foreach ($buildings as $building){
                if($building->unit_count>1){
                    $building->gen_dorm_rule='{简称}-{单元}-{楼层}{流水号:00}';
                }else{
                    $building->gen_dorm_rule='{简称}-{楼层}{流水号:00}';
                }
                $building->save();
            }
        }

        $q = $this->ask('truncate dorms?','n');
        if(strtolower($q)=='y'){
            \DB::unprepared('SET FOREIGN_KEY_CHECKS=0');
            Dorm::truncate();
        }


        $q = $this->ask('gen dorms?','n');
        if(strtolower($q)=='y'){
            $buildings = Building::all();
            foreach ($buildings as $building){
                if($building->dorm_type_id){
                    Utils::gen_dorms($building->id);
                }
            }
        }

        $q = $this->ask('truncate dorm_meters?','n');
        if(strtolower($q)=='y'){
            \DB::unprepared('SET FOREIGN_KEY_CHECKS=0');
            DormMeter::truncate();
        }


        $q = $this->ask('insert dorm_meters test data?','n');
        if(strtolower($q)=='y'){
            $dorms = Dorm::shouldInstallMeter()->get();
            $meterTypes = MeterType::all();
            foreach ($dorms as $dorm) {
                foreach ($meterTypes as $meterType){
                    $nfc_id = $dorm->code.'-'.$meterType->abbr.'1';
                    $meter=[
                        'dorm_id'=>$dorm->id,
                        'meter_type_id'=>$meterType->id,
                        'nfc_id'=>$nfc_id,
                        'last_reading'=>0,
                        'last_reading_date'=>Carbon::now()->firstOfMonth()->format('Y-m-d'),
                        'unit'=>$meterType->unit,
                    ];
                    DormMeter::create($meter);
                }

            }
        }



        $this->call('build:period');

        $this->info('test date inserted finished');
    }
}
